$NG.AllReady(function (page, { useAction,useAttachmentReturn, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const config = {
        asrid:'',
        u_lxzt_init:''
    }
    const user = $NG.getUser()
    const mstform = $NG.getCmpApi("p_form_kjxmgl_m")
    
    if(page.oType == 'add'){
        const u_lxzt = mstform.getItem("u_lxzt").getValue()
        u_lxzt || mstform.getItem("u_lxzt").setValue('03')
    }
    if(page.oType == 'add' || page.oType == 'edit'){
        selectUlxzt()
    }

    useBeforeClick(async ()=>{
        const result = await tableAttachInit()
        if(!result){
            return false
        }
        const num = result.data.attachmentRecordList.length
        if(num == 0){
            $NG.alert("您还没有上传附件，请上传附件！")
            return false
        }
        return true
    },'save')

    // 监听单据附件弹窗返回
    useAttachmentReturn(({ args }) => {
        // 附件弹框点击的确认还是取消
        console.log('status', args[0].closeStatus);
        // 附件的asrSessionGuid
        config.asrid = args[0].asrSessionGuid
        console.log("asrid", args[0].asrSessionGuid);
    });

    useValuesChange(()=>{
        getProjectInfoByPhid()
    },"p_form_kjxmgl_m.u_ytxfmc")

    // 立项状态
    useValuesChange(()=>{
        selectUlxzt()
    },"p_form_kjxmgl_m.u_lxzt")

    function selectUlxzt(){
        const u_lxzt = mstform.getItem("u_lxzt").getValue()
        if(u_lxzt && u_lxzt != '03'){
            $NG.updateUI((updater)=>{
                updater.fieldSetForm.p_form_kjxmgl_m.u_lxzt.setProps({
                    disabled:true
                })
            })
        }
        config.u_lxzt_init = u_lxzt
    }

    function getProjectInfoByPhid(){
        const phid = mstform.getItem("u_ytxfmc").getValue()
        if(!phid) return
        $NG.execServer("getProjectInfoByPhid",{phid},(res)=>{
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            $NG.updateState((updater)=>{
                updater.data.p_form_kjxmgl_m.setProps({
                    bill_name:datas.bill_name,
                    ng_insert_dt:datas.start_date,
                    u_xmjsrq:datas.end_date
                })
            })
        })
    }

    // 查询附件，找到附件的数量
    async function tableAttachInit() {
        if (!config.asrid) {
            $NG.alert("请上传附件")
            return false
        }
        const origin = location.origin
        const router = "/JFileSrv/reactAttach/tableAttachInit"
        const param = {
            asrSessionGuid: config.asrid,
            busTypeCode: "Disclose_First",
            asrFill: user.userID,
            asrFillName: user.userName,
            asrTable: 'FirstLevelDisclosureFrom',
            asrCode: page.oType == 'add' ? config.asrid : mstform.getValues().phid,
            orgId: user.orgID,
            colAttach: '0',
            uCode: '0001'
        }
        const query = new URLSearchParams(param).toString();
        const url = `${origin}${router}?${query}`;
        try {
            const res = await fetch(url, {
                method: 'GET',
                headers: {
                    "dbToken": '0001'
                }
            });
            if (!res.ok) {
                throw new Error(`HTTP 错误：${res.status}`);
            }
            // 假设接口返回 JSON
            const data = await res.json();
            console.log("返回数据：", data);
            // 根据业务逻辑处理
            if (data.code==200) {
                return data
            } else {
                console.warn("请求返回异常：", data.message || data);
                $NG.alert("获取附件数量发生异常，请联系管理员！")
                return false
            }
        } catch (err) {
            console.error("请求失败：", err);
            $NG.alert("获取附件数量发生异常，请联系管理员！")
            return false
        }

    }
})